package org.example.scoresystem_backen.controller;

import org.example.scoresystem_backen.common.Result;
import org.example.scoresystem_backen.util.AuthUtil;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器
 */
@RestController
@RequestMapping("/api/test")
@CrossOrigin(origins = "*")
public class TestController {
    
    /**
     * 健康检查接口
     * @return 系统状态
     */
    @GetMapping("/health")
    public Result<Map<String, Object>> health() {
        Map<String, Object> data = new HashMap<>();
        data.put("status", "UP");
        data.put("timestamp", LocalDateTime.now().toString());
        data.put("message", "学生成绩管理系统运行正常");
        return Result.success("系统健康检查通过", data);
    }
    
    /**
     * 简单的Hello World接口
     * @return 问候信息
     */
    @GetMapping("/hello")
    public Result<String> hello() {
        return Result.success("Hello, 学生成绩管理系统!");
    }

    /**
     * CORS测试接口
     * @return CORS测试结果
     */
    @GetMapping("/cors")
    public Result<Map<String, Object>> corsTest() {
        Map<String, Object> data = new HashMap<>();
        data.put("message", "CORS配置正常");
        data.put("timestamp", LocalDateTime.now().toString());
        data.put("origin", "允许跨域访问");
        return Result.success("CORS测试成功", data);
    }

    /**
     * 需要认证的测试接口
     * @return 当前用户信息
     */
    @GetMapping("/auth-test")
    public Result<Map<String, Object>> authTest() {
        Map<String, Object> data = new HashMap<>();

        // 获取当前用户信息
        Integer userId = AuthUtil.getCurrentUserId();
        if (userId != null) {
            data.put("authenticated", true);
            data.put("userId", userId);
            data.put("username", AuthUtil.getCurrentUsername());
            data.put("userType", AuthUtil.getCurrentUserType());
        } else {
            data.put("authenticated", false);
        }

        data.put("timestamp", LocalDateTime.now().toString());
        data.put("message", "JWT认证测试成功");

        return Result.success("认证测试通过", data);
    }
    
    /**
     * 测试POST接口
     * @param data 请求数据
     * @return 响应数据
     */
    @PostMapping("/echo")
    public Result<Map<String, Object>> echo(@RequestBody Map<String, Object> data) {
        Map<String, Object> response = new HashMap<>();
        response.put("received", data);
        response.put("timestamp", LocalDateTime.now());
        return Result.success("数据回显成功", response);
    }
}
